import org.apache.spark.{SparkConf, SparkContext}

import scala.reflect.io.Directory

object WordCount extends App {

  //-------------------use SparkContext--------------------
  val conf = new SparkConf().setAppName("word-count-scala").setMaster("local[2]")
  val sc = new SparkContext(conf)

  //指定resources 目录下的文件
  var filePath = "file:///" +
    Directory.Current.get.toString() +
    "\\data\\world_count.txt"


  val wordCounts = sc.textFile(filePath).flatMap(line => line.split(" "))
    .map(x => (x, 1))
    .reduceByKey(_ + _)

  wordCounts.foreach(wordCount => println(wordCount._1 + "出现" + wordCount._2 + " 次"))
}
